"""
    python 3.7 之前用  asyncio.ensure_future()
    python 3.7 之后用  asyncio.create_task()
"""

import asyncio


async def func(num):
    print(num, 1)
    await asyncio.sleep(1)
    print(num, 2)
    return 3


async def main():
    print("main start")
    # 创建task对象
    task_list = [
        #这里可以给每个任务取一个名字，便于后续进行任务的监控
        asyncio.create_task(func(1),name="n1"),
        asyncio.create_task(func(2),name="n2")
    ]

    print("main end")

    done, pending = await asyncio.wait(task_list, timeout=None)
    for task in done:
        #根据不同任务做不同处理
        if task.get_name().__eq__('n1'):
            print(task.result())
        if task.get_name().__eq__('n2'):
            print("n2",task.result())


asyncio.run(main())
